// 算命相关路由

const express = require('express');
const router = express.Router();
const { validateFortuneInput } = require('../utils/validation');
const FortuneService = require('../services/fortuneService');

// POST /api/fortune - 算命接口
router.post('/', validateFortuneInput, async (req, res) => {
    try {
        const { name, birthDate, gender, city } = req.body;
        
        // 解析出生日期
        const date = new Date(birthDate);
        const birthYear = date.getFullYear();
        const birthMonth = date.getMonth() + 1;
        const birthDay = date.getDate();
        
        // 计算生肖和星座
        const zodiac = FortuneService.calculateZodiac(birthYear);
        const constellation = FortuneService.calculateConstellation(birthMonth, birthDay);
        
        // 生成运势（包含天气信息）
        const fortune = await FortuneService.generateFortune(zodiac, constellation, name, city);
        
        // 返回完整结果
        res.json({
            success: true,
            data: {
                userInfo: {
                    name,
                    birthDate,
                    gender,
                    city: city || '当前位置'
                },
                zodiac,
                constellation,
                fortune
            }
        });
        
    } catch (error) {
        console.error('Fortune calculation error:', error);
        res.status(500).json({
            success: false,
            error: '算命服务暂时不可用，请稍后重试'
        });
    }
});

module.exports = router;